/*
 * AALSensorItem.c
 *
 *  Created on: 10.06.2011
 *      Author: sili
 */

#include "AALSensorItem.h"

static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("at.sili.aalsystem.model.AALSensorItem"));

AALSensorItem::AALSensorItem(const int _id, Controller * _controller)
			  :AALLocatorItem(_id,_controller){
	AAL_TRACE(logger,"Constructor globalId:%d", AALItem::globalId);
	this->visibility = 100;		// sensors actors always visible
	this->type.clear();
	this->type.append("sensor");
}

AALSensorItem::~AALSensorItem() {
	AAL_TRACE(logger,"Destructor globalId:%d", AALItem::globalId);
}

/*
 * OVERWRITTES THE AALLOCATORITEM SETHOTKEY (WHICH IS NONE)
 */
void AALSensorItem::setHotkey(int _hotkey){
	AALItem::setHotkey(_hotkey);
}

/*
 * Same as AALItem but for sensors and actuators the position is in the upper right corner
 */
void AALSensorItem::renderHotkeys(DrawPane * _target, const int _renderDepth, int _currentDepth){
	int w = this->image.columns();
	int h = this->image.rows();

	if(this->visibility != 0 && this->hotkey != -1 && w > 0 && h > 0){
		AALLocation location;
		location.left = this->area.left + this->area.width;
		location.top = this->area.top;
		_target->DrawHotkey(location, this);
	}

	int childrenDepth = _currentDepth + 1;
	if(_currentDepth <= _renderDepth){
		vector<AALItem*>::iterator entry = this->children.begin();
		while(entry != this->children.end()){
			(*entry)->renderHotkeys(_target, _renderDepth, childrenDepth);
			entry++;
		}
	}
}


void AALSensorItem::changeValue(int _value){
	// This is different form locator. Sensors are never hidden in opposite to locators
	AAL_DEBUG(logger,"ChangeValue actuatorId:%d value:%d", this->getId(), _value);
	this->currentValue = _value;
	this->controller->publish(doReaload,"",this);
}

void AALSensorItem::visualizeSelf(Visualizer * _visualizer, AALAction * _action){

	if( _action->type == doInfo ||
		_action->type == doWarn ||
		_action->type == doAlarm){
		AALItem::visualizeSelf(_visualizer, _action);
		return;
	}

	// Parent does the default stuff
	AALLocatorItem::visualizeSelf(_visualizer, _action);

	// Add current value
	const cFont * font = cFont::GetFont(fontOsd);
	if(_visualizer->getInfoPane() != NULL){
		char integerBuffer[10];
		snprintf(integerBuffer, (size_t)9, "%d", this->currentValue);

		string textline(tr("Aktueller Wert:"));
		textline.append(" ");

		Template * cp = this->controller->getTemplateFactory()->getTemplate(this->templateName.c_str());

		if(cp != NULL){
			if(this->currentValue == cp->minValue){
				textline.append(cp->minName.c_str());
			} else if(this->currentValue == cp->maxValue){
				textline.append(cp->maxName.c_str());
			} else {
				textline.append(integerBuffer);
				textline.append(" ");
				textline.append(cp->unit.c_str());
			}
		} else {
			textline.append(integerBuffer);
		}
		this->textArea.top =this->textArea.top + this->textArea.height;
		_visualizer->getInfoPane()->DrawText(this->textArea, textline.c_str(), _visualizer->getTextColor(), clrTransparent, font);
	}
}
